/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-14
 * Time: 10:54
 */
public class Demo3 {
    private static int count=0;
    private  static Object locker = new Object();

    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }

            }
        },"A");
        Thread t2=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }

            }
        },"B");
        Thread t3=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=2){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                    System.out.println(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }

            }
        },"C");
        t1.start();
        t2.start();
        t3.start();

    }
}
